using AvalonAdmin.BaseViewModel;
using AvalonAdmin.Consts;
using AvalonAdmin.Helpers;
using AvalonAdminDto.CarParts.CarPartsDtos;
using AvalonAdminDto.CarParts.CarPartsTypeDtos;
using AvalonAdminDto.Common;
using Avalonia.Threading;
using D.UtilCore.Dtos;
using System;
using System.Threading.Tasks;

namespace AvalonAdmin.Pages.CarParts.ViewModels.CarPartsType
{
    /// <summary>
    /// 配件类型修改视图模型
    /// </summary>
    public class CarPartsTypeModifyViewModel : BaseModifyViewModel
    {
        #region 页面属性及初始化数据
        private string? _id;
        private RepCarPartsType _currentCarPartsType;

        public CarPartsTypeModifyViewModel(string? id, ViewStatus? viewStatus)
        {
            _id = id;
            _currentCarPartsType = new RepCarPartsType();
            SetupFormStatus(viewStatus);

            // 如果有ID，则加载数据
            if (!string.IsNullOrEmpty(_id))
            {
                _ = Task.Run(async () => await QueryAsync());
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        public override async Task<bool> QueryAsync()
        {
            if (string.IsNullOrEmpty(_id)) return false;

            try
            {
                // 调用后端接口
                var res = await HttpHelper<ApiResult<RepCarPartsType>>.GetAsync(ApiUrl.GetCarPartsType + "/" + _id);
                //  判断结果
                if (res.Code == StatusCodeEnum.Waring)
                {
                    ShowError(res.Msg);
                }
                else if (res.Code == StatusCodeEnum.Error)
                {
                    ShowError("服务器异常");
                }
                else
                {
                    // 绑定数据源到UI 
                    if (res.Data != null)
                    {
                        await Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            Name = res.Data.Data.Name;
                            TypeCode = res.Data.Data.TypeCode;
                            EnglishName = res.Data.Data.EnglishName;
                        });
                    }
                } 
            }
            catch (Exception ex)
            {
                ShowError($"查询失败: {ex.Message}");
            }
            return true;
        }
        #endregion

        #region 数据编辑属性

        private string? name;
        /// <summary>
        /// 名称
        /// </summary>
        public string? Name
        {
            get => name;
            set => SetProperty(ref name, value);
        }

        private string? englishName;
        /// <summary>
        /// 英文名称
        /// </summary>
        public string? EnglishName
        {
            get => englishName;
            set => SetProperty(ref englishName, value);
        }

        private string? typeCode;
        /// <summary>
        /// 编码
        /// </summary>
        public string? TypeCode
        {
            get => typeCode;
            set => SetProperty(ref typeCode, value);
        }

        private string? description;
        /// <summary>
        /// 备注
        /// </summary>
        public string? Description
        {
            get => description;
            set => SetProperty(ref description, value);
        }
        #endregion

        #region 提交，重置

        /// <summary>
        /// 提交数据
        /// </summary>
        /// <returns></returns>
        protected override async Task SubmitAsync()
        {
            try
            {
                // 参数验证判断
                if (string.IsNullOrWhiteSpace(Name))
                {
                    ShowWarning("配件类型名称不能为空"); return;
                }

                // 参数验证判断
                if (string.IsNullOrWhiteSpace(TypeCode))
                {
                    ShowWarning("配件类型编码不能为空"); return;
                }


                // 根据状态判断
                if (string.IsNullOrEmpty(_id))
                {
                    // 调用后端接口 
                    var res = await HttpHelper<int>.PostAsync(ApiUrl.AddCarPartsType, new ReqAddCarPartsType
                    {
                        Name = Name,
                        EnglishName = EnglishName,
                        TypeCode = TypeCode,
                    });

                    //  判断结果
                    HandleApiResponse(res, "新增成功", "新增失败");
                }
                else
                {
                    // 调用后端接口 
                    var res = await HttpHelper<int>.PutAsync($"{ApiUrl.UpdateCarPartsType}", new ReqUpdateCarPartsType
                    {
                        Id = _id,
                        Name = Name,
                        EnglishName = EnglishName,
                        TypeCode = TypeCode,
                    });
                    // 判断结果
                    HandleApiResponse(res, "修改成功", "修改失败");
                }
            }
            catch (Exception ex)
            {
                ShowError($"操作失败: {ex.Message}");
            }
        }

        /// <summary>
        /// 重置表单
        /// </summary>
        protected override void Reset()
        {
            // 实现重置表单逻辑
            _currentCarPartsType = new RepCarPartsType();
            OnPropertyChanged(nameof(Name));
            OnPropertyChanged(nameof(EnglishName));
            OnPropertyChanged(nameof(TypeCode));
            OnPropertyChanged(nameof(Description));
        }

        #endregion
    }
}